<?php
/**
 * Minhlog Domain_Model 文章标签类
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Domain
 * @package    Domain_Model
 * @copyright  Copyright (c) 2009-2010 Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: Tag.php 2 2010-01-17 13:49:08Z Minh.Shu $
 *
 */

class Domain_Model_Tag
{
    private $_tagArticleDataMapper;
    private $_tagArticleEntity;
    private $_tagDataMapper;
    private $_tagEntity;

    public function  __construct()
    {
        $this->_tagArticleDataMapper = new Domain_DataMapper_Tag_Article();
        $this->_tagArticleEntity = new Domain_Entity_Tag_Article();
        $this->_tagArticleDataMapper->setEntity($this->_tagArticleEntity);
        $this->_tagDataMapper = new Domain_DataMapper_Tag();
        $this->_tagEntity = new Domain_Entity_Tag();
        $this->_tagDataMapper->setEntity($this->_tagEntity);
    }

    /**
     * 根据标签ID查询信息
     * @param integer $tagId
     * @return object|void
     */
    public function getTagById($tagId)
    {
        $tagId = (int) $tagId;
        return $this->_tagDataMapper->findByPrimaryId($tagId);
    }

    /**
     * 根据标签ID查询关联文章数据
     * @param integer $tagId
     * @return object|void
     */
    public function getRelationByTag($tagId)
    {
        $tagId = (int) $tagId;
        $result = $this->_tagArticleDataMapper->findRelationByTag($tagId);
        return $result;
    }

    /**
     * 查询全部标签
     * @return object|void
     */
    public function getAllTag()
    {
        return $this->_tagDataMapper->findAllTag();
    }

    /**
     * 根据文章ID和标签ID查询关系
     * @param integer $articleId
     * @param integer $tagId
     * @return
     */
    public function checkTagRelation($articleId, $tagId)
    {
        return $this->_tagArticleDataMapper->findTagRelation($articleId, $tagId);
    }

    /**
     * 保存文章标签
     * @param array $data
     * @param integer $primary
     * @return
     */
    public function saveTag($data, $primary = 0)
    {
        return $this->_tagDataMapper->save($data, $primary);
    }

    /**
     * 保存文章标签关系
     * @param array $data
     * @return
     */
    public function saveTagRelation($data)
    {
        return $this->_tagArticleDataMapper->save($data);
    }

    /**
     * 删除标签同时删除文章关联关系
     * @param integer $primaryId
     * @return
     */
    public function delTag($primaryId)
    {
        $primaryId = (int) $primaryId;
        return $this->_tagDataMapper->deleteDependent($primaryId);
    }

    /**
     * 删除文章关联的标签关系
     * @param integer $articleId
     * @return
     */
    public function delTagRelateion($articleId)
    {
        $articleId = (int) $articleId;
        $where = 'article_id = '.$articleId;
        return $this->_tagArticleDataMapper->deleteWhere($where);
    }
}